package com.ibt.dao;
	import java.util.ArrayList;
	import java.util.List;

	import org.hibernate.Query;
	import org.hibernate.Session;
	import org.hibernate.Transaction;

import com.ibt.entity.Module;
	import com.ibt.entity.ProjectEntry;
import com.ibt.entity.ProjectEntry;
import com.ibt.util.ApplicationContext;

	public class ProjectEntryDAO {
		static Transaction tx;

		public static int insert(ProjectEntry b) throws Exception {

			Session session = startOperations();
			session.saveOrUpdate(b);
			session.getTransaction().commit();
			endOperations(session);
			return 0;
		}

		public static int update(ProjectEntry b) throws Exception {
			Session session = startOperations();
			Query qry = session
					.createQuery("update ProjectEntry set current_points=:points where business_id=:busid and customer_id=:custid");
			//qry.setInteger("points", b.getCurrentPoints());
			////qry.setInteger("busid", b.getBusiness().getBusinessId());
			//qry.setInteger("custid", b.getCustomer().getCustomerId());
			Object ob = qry.executeUpdate();
			session.getTransaction().commit();
			endOperations(session);
			return 0;
		}

		public static int delete(ProjectEntry b) throws Exception {
			Session session = startOperations();
			session.delete(b);
			// tx.commit();
			endOperations(session);
			return 0;
		}

		private static void endOperations(Session session) {
			session.close();
			session = null;
		}

		private static Session startOperations() throws Exception {
			Session session = ApplicationContext.getHiberSession().openSession();
			tx = session.beginTransaction();
			return session;
		}

		public static List<ProjectEntry> findAll() throws Exception {
			List<ProjectEntry> custObjs = new ArrayList<ProjectEntry>();
			List objs = null;
			Session session = startOperations();
			Query query = session.createQuery("from ProjectEntry");
			objs = query.list();
			for (Object o : objs) {
				custObjs.add((ProjectEntry) o);
			}
			endOperations(session);
			return custObjs;
		}

		public static ProjectEntry findById(int i) throws Exception {
			Session session = startOperations();
			ProjectEntry c = (ProjectEntry) session.load(
					ProjectEntry.class, i);
			endOperations(session);
			return c;
		}
		public static ProjectEntry getByName( String projname,int busid) throws Exception {
			Session session = startOperations();
			Query qry = session.createQuery("from ProjectEntry where project_name=:projname and business_id=:bid");
			qry.setString("projname",projname);
			qry.setInteger("bid", busid);
			Object obj = qry.uniqueResult();
			ProjectEntry modObj=(ProjectEntry)obj;
			System.out.println(" project obj is " + obj);

			endOperations(session);
			return modObj;

		}

		public static Object getByCustBusId(int id, int i) throws Exception {
			Session session = startOperations();
			System.out.println("id in dao " + id);
			Query qry = session
					.createQuery("from ProjectEntry where customer_id=:id and business_id=:bid");
			qry.setInteger("id", id);
			qry.setInteger("bid", i);
			Object obj = qry.uniqueResult();
			System.out.println("obj is " + obj);

			endOperations(session);
			return obj;

		}

		public static List<ProjectEntry> getBusinessCustMap(int userid)
				throws Exception {
			Session session = startOperations();
			System.out.println("id in dao " + userid);
			Query qry = session
					.createQuery("from ProjectEntry where customer_id=:userid");
			qry.setInteger("userid", userid);
			List<Object> obj = qry.list();
			ArrayList<ProjectEntry> lst = new ArrayList<ProjectEntry>();
			for (Object o : obj) {
				System.out.println("obj is " + obj);
				lst.add((ProjectEntry) o);
			}
			endOperations(session);
			return lst;

		}

		public static List<ProjectEntry> findByBusId(int bsid) throws Exception {

			Session session = startOperations();
			Query qry = session
					.createQuery("from ProjectEntry where business_id=:id");
			qry.setInteger("id", bsid);
			List<ProjectEntry> listBC = qry.list();
			endOperations(session);
			return listBC;
		}

	}



